package com.xyzwps.navia.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@ConfigurationPropertiesScan
@SpringBootApplication
public class GatewayApp {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApp.class, args);
    }

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ServiceProperties services) {
        return builder.routes()
                .route("content-route", r -> r.path("/apis/content/**")
                        .filters(f -> f.rewritePath("/apis/content/(?<path>.*)", "/${path}"))
                        .uri(services.getContent().getHost()))
                .route("user-route", r -> r.path("/apis/user/**")
                        .filters(f -> f.rewritePath("/apis/user/(?<path>.*)", "/${path}"))
                        .uri(services.getUser().getHost()))
                .build();
    }
}
